package top.callname.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSON;

import top.callname.bean.AdminBean;
import top.callname.bean.ClassBean;
import top.callname.bean.ConstanstBean;
import top.callname.bean.RspInfoBean;
import top.callname.service.AdminService;
import top.callname.service.ClassService;
import top.callname.service.StudentService;
import top.callname.service.impl.AdminServiceImpl;
import top.callname.service.impl.ClassServiceImpl;
import top.callname.service.impl.StudentServiceImpl;
import top.callname.util.BaseUtil;
import top.callname.util.DateUtil;


@WebServlet("/admin/teacher")
public class TeacherServlet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
	private StudentService studentService = new StudentServiceImpl();
	private AdminService adminService = new AdminServiceImpl();
	private ClassService classService = new ClassServiceImpl();
	private Logger logger = Logger.getLogger(TeacherServlet.class);

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		String method = request.getParameter("method");
		
		if("teacher-delete".equals(method)){
			String pid = request.getParameter("pid");
			RspInfoBean rsp = adminService.deleteTeacher(Integer.valueOf(pid));
			logger.info("删除用户："+rsp.toString());
			out.write(JSON.toJSONString(rsp));
		}else if("teacher-update".equals(method)){
			
			String pid = request.getParameter("pid");
			request.setAttribute("teacherInfo",adminService.getAdminByPid(Integer.valueOf(pid)));
			request.getRequestDispatcher("admin-teacher-update.jsp").forward(request,response);
			
		}else if("teacher-save".equals(method)){
			
			String fullname = request.getParameter("fullname");
			String pid = request.getParameter("pid");
			String username = request.getParameter("username");
			String passwd = request.getParameter("passwd");
			AdminBean adminBean = new AdminBean();
			adminBean.setFullname(fullname);
			adminBean.setPasswd(passwd);
			adminBean.setUsername(username);
			adminBean.setPid(Integer.valueOf(pid));
			RspInfoBean rsp = adminService.updateUserInfo(adminBean);
			logger.info("更新用户："+rsp.toString());
			if(ConstanstBean.RSP_SUCCESS.equals(rsp.getRspCode())){
				response.sendRedirect("teacher");
			}else{
				request.setAttribute("adminInfo", adminBean);
				request.setAttribute("regError", rsp.getRspDesc());
				request.getRequestDispatcher("admin-teacher-update.jsp").forward(request,response);  
			}
			
		}else if("teacher-class-update".equals(method)){
			//教师管理的班级
			String pid = request.getParameter("pid");
			request.setAttribute("allClassList", classService.getAll());
			request.setAttribute("teacherInfo", adminService.getAdminByPid(Integer.valueOf(pid)));
			request.getRequestDispatcher("admin-teacherClass-update.jsp").forward(request,response);  
			
		}else if("teacher-class-save".equals(method)){
			//保存修改
			String pid = request.getParameter("pid");
			String cids[] = request.getParameterValues("cid");
			RspInfoBean rsp = adminService.updateAdminClass(Integer.valueOf(pid) , cids);
			out.write(JSON.toJSONString(rsp));
			
		}else if("class".equals(method)){
			//班级管理
			String pageNo = request.getParameter("pageNo");
			int page = BaseUtil.getPageNo(pageNo);
			request.setAttribute("page", classService.getClassByPage(page));
			request.getRequestDispatcher("admin-class.jsp").forward(request,response);
		}else if("class-add".equals(method)){
			//班级管理
			String className = request.getParameter("className");
			ClassBean classBean = new ClassBean();
			classBean.setClassName(className);
			classBean.setCreatetime(DateUtil.dateToStrLong(new Date()));
			RspInfoBean rsp = classService.addClass(classBean);
			out.write(JSON.toJSONString(rsp));
			
		}else if("class-delete".equals(method)){
			//删除班级
			String cid = request.getParameter("cid");
			RspInfoBean rsp = classService.deleteClass(Integer.valueOf(cid));
			out.write(JSON.toJSONString(rsp));

		}else if("class-student".equals(method)){
			//班级学生管理
			//分页查询
			String pageNo = request.getParameter("pageNo");
			String cid = request.getParameter("cid");
			int page = BaseUtil.getPageNo(pageNo);
			request.setAttribute("page", studentService.getListPage(page, Integer.valueOf(cid)));
			request.setAttribute("currentClassId", cid);
			request.setAttribute("classInfo",classService.getByCid(Integer.valueOf(cid)));
			request.getRequestDispatcher("admin-class-student.jsp").forward(request,response);
			
		}else{
			String pageNo = request.getParameter("pageNo");
			int page = BaseUtil.getPageNo(pageNo);
			request.setAttribute("page", adminService.getAdminByPage(page));
			request.getRequestDispatcher("admin-teacher.jsp").forward(request,response);
		}
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
	
	

}
